﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcmyjobs.Web.Admin
{
    public partial class ListEnterpriseMember : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["AdminLogin2"] != null)
                {
                    int atid = Convert.ToInt32(Session["AdminLogin2"]);

                    Bind(atid);
                }
                else
                {
                    Maticsoft.Common.MessageBox.ResponseScript(this, "alert('请先登陆！');top.location.href='adminlogin2.aspx'");
                }
            }
        }

        private void Bind(int atid)
        {
            int count = 0;

            DataSet ds = new lcmyjobs.BLL.enterprise_level().GetLevelInfo(atid, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out count);

            this.lbl_count.Text = ds.Tables[1].Rows[0][0].ToString();

            this.AspNetPager1.RecordCount = count;

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            if (Session["AdminLogin2"] != null)
            {
                int atid = Convert.ToInt32(Session["AdminLogin2"]);

                Bind(atid);
            }
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_check")
            {
                if (e.CommandArgument != null)
                {
                    int lid = Convert.ToInt32(e.CommandArgument);
                    int mid = 0;
                    int bid = 0;
                    int membertimecount = 0;

                    GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;

                    Label lb = row.FindControl("lbl_info") as Label;

                    string[] str = lb.Text.Split(',');

                    if (str.Length > 0)
                    {
                        mid = Convert.ToInt32(str[0]);
                        bid = Convert.ToInt32(str[1]);
                        membertimecount = Convert.ToInt32(str[2]);


                        DateTime now = DateTime.Now;
                        DateTime memebertime = now.AddDays(membertimecount);

                        if ((new lcmyjobs.BLL.enterprise_basic().UpdateMemberLevel(bid, mid, memebertime) > 0))
                        {
                            if (new lcmyjobs.BLL.enterprise_level().UpdateLevel(lid,now,1)> 0 )
                            {
                                //Maticsoft.Common.MessageBox.Show(this, "升级成功！");

                                if (Session["AdminLogin2"] != null)
                                {
                                    int atid = Convert.ToInt32(Session["AdminLogin2"]);

                                    Bind(atid);
                                }

                            }
                        }
                       
                    }




                
                }
            }
        }
        /// <summary>
        /// 获取主键ID列表
        /// </summary>
        /// <returns></returns>
        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.GridView1.Rows[i].FindControl("cbo_rid");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (GridView1.DataKeys[i].Value != null)
                    {
                        idlist += GridView1.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }
        /// <summary>
        /// 获取Email邮件列表
        /// </summary>
        /// <returns></returns>
        private string GetEmailAddlist()
        {
            string emaillist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.GridView1.Rows[i].FindControl("cbo_rid");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;

                    Label lb = this.GridView1.Rows[i].FindControl("lbl_email") as Label;

                    if (!string.IsNullOrEmpty(lb.Text))
                    {
                        emaillist += lb.Text + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                emaillist = emaillist.Substring(0, emaillist.LastIndexOf(","));
            }
            return emaillist;
        }

        /// <summary>
        /// 获取刷新列表
        /// </summary>
        /// <returns></returns>
        private string GetUplist()
        {
            string emaillist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.GridView1.Rows[i].FindControl("cbo_rid");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;

                    Label lb = this.GridView1.Rows[i].FindControl("lbl_info") as Label;

                    if (!string.IsNullOrEmpty(lb.Text))
                    {
                        emaillist += lb.Text + "|";
                    }
                }
            }
            if (BxsChkd)
            {
                emaillist = emaillist.Substring(0, emaillist.LastIndexOf("|"));
            }
            return emaillist;
        }
        public string GetBH(string str)
        {
            return str.ToString().PadLeft(6, '0');
        }
        //发送邮件
        protected void lbn_sendEmail_Click(object sender, EventArgs e)
        {
            string emaillist = this.GetEmailAddlist();
            if (emaillist.Length > 0)
            {
                Session["receiveEmailAdd"] = emaillist;
                Response.Redirect("EmailSend.aspx");
          

            }
            else
            {
                //Maticsoft.Common.MessageBox.Show(this, "请选择所要发送邮件的单位！");
            }
        }

        protected void lbn_update_Click(object sender, EventArgs e)
        {
            string[] infos = this.GetUplist().Split('|');

            bool flag = false; 

            lcmyjobs.BLL.enterprise_basic eb = new BLL.enterprise_basic();
            lcmyjobs.BLL.enterprise_level el = new BLL.enterprise_level();

            foreach (string info in infos)
            {
                string[] str = info.Split(',');

                if (str.Length > 0 )
                {
                    int mid = Convert.ToInt32(str[0]);
                    int bid = Convert.ToInt32(str[1]);
                    int membertimecount = Convert.ToInt32(str[2]);
                    int lid = Convert.ToInt32(str[3]);

                    DateTime now = DateTime.Now;
                    DateTime memebertime = now.AddDays(membertimecount);

                    if ((eb.UpdateMemberLevel(bid, mid, memebertime) > 0))
                    {
                        if (el.UpdateLevel(lid, now, 1) > 0)
                        {
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }

                if (flag)
                {
                    //Maticsoft.Common.MessageBox.Show(this, "升级成功！");

                    if (Session["AdminLogin2"] != null)
                    {
                        int atid = Convert.ToInt32(Session["AdminLogin2"]);

                        Bind(atid);
                    }
                }
            }
        }

        protected void lbn_del_Click(object sender, EventArgs e)
        {
            string Idlist = this.GetSelIDlist();

            if (!string.IsNullOrEmpty(Idlist))
            {
                lcmyjobs.BLL.enterprise_level el = new BLL.enterprise_level();
                lcmyjobs.BLL.enterprise_basic eb = new BLL.enterprise_basic();

                string[] infos = this.GetUplist().Split('|');

                bool flag = false; 

                foreach (string info in infos)
                {
                    string[] str = info.Split(',');

                    if (str.Length > 0)
                    {
                        int mid = Convert.ToInt32(str[0]);
                        int bid = Convert.ToInt32(str[1]);
                        int membertimecount = Convert.ToInt32(str[2]);
                        int lid = Convert.ToInt32(str[3]);

                        DateTime now = DateTime.Now;
                     
                        if ((eb.UpdateMemberLevel(bid, 1, now) > 0))
                        {
                            if (el.DeleteList(lid.ToString()))
                            {
                                flag = true;
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                    }
                }



                if (flag)
                {
                    //Maticsoft.Common.MessageBox.Show(this, "删除单位成功！");

                    if (Session["AdminLogin2"] != null)
                    {
                        int atid = Convert.ToInt32(Session["AdminLogin2"]);

                        Bind(atid);
                    }
                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要删除的单位！", "ListEnterpriseTop.aspx");
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells[8].Text == "未审核")
            {
                e.Row.Cells[8].ControlStyle.ForeColor = System.Drawing.Color.Red;
            }
        }

      

        
    }
}